#! /usr/bin/python

# 引入GPIO模块
import RPi.GPIO as GPIO
import time

# 指定编码方式 + 关闭警告信息
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# 设置输入引脚
btn_pin = 1

# 输入模式, 并设置下拉
GPIO.setup(btn_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# 检测LOW -> HIGH的变化
GPIO.add_event_detect(btn_pin, GPIO.RISING, bouncetime=200)

# 按键初始打开状态: False
state = False
try:
    while True:
        if GPIO.event_detected(btn_pin):
            state = not state
            print("current state: ", state)
        time.sleep(0.01)
except Exception as e:
    print(e)

# 清理占用的GPIO资源
GPIO.cleanup()
